package com.telit.firecommand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telit.common.exception.ForestBaseException;
import com.telit.firecommand.dao.CommFireAlarmDao;
import com.telit.firecommand.dao.CommFireHandleDao;
import com.telit.firecommand.dto.FireHandleDTO;
import com.telit.firecommand.entity.CommFireHandle;
import com.telit.firecommand.service.FireHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2021/8/3 18:49
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FireHandleServiceImpl extends ServiceImpl<CommFireHandleDao, CommFireHandle> implements FireHandleService {

    @Autowired
    private CommFireAlarmDao commFireAlarmDao;

    @Override
    public void add(CommFireHandle fireHandleInfo) {
        commFireAlarmDao.updatePrimaryLevel(fireHandleInfo.getFireId(), fireHandleInfo.getFireJudgmentLevel());
        if (baseMapper.insert(fireHandleInfo) <= 0) {
            throw new ForestBaseException("添加应急预案失败");
        }
    }

    /**
     * 分页条件查询火情处理信息
     *
     * @param current
     * @param size
     * @param fireHandleInfo
     *
     * @return
     */
    @Override
    public IPage<CommFireHandle> selectByPage(Integer current, Integer size, CommFireHandle fireHandleInfo) {
        IPage<FireHandleDTO> fireHandleInfoDTOIPage = new Page<>(current, size);
        return baseMapper.selectPageByCondition(fireHandleInfoDTOIPage, fireHandleInfo);
    }

    /**
     * 无分页条件查询火情处理信息
     *
     * @param fireHandleInfo
     *
     * @return
     */
    @Override
    public List<CommFireHandle> selectByNoPage(CommFireHandle fireHandleInfo) {
        return baseMapper.selectByConditionNoPage(fireHandleInfo);
    }

    /**
     * 根据火情id查询火情处理信息
     *
     * @return
     */
    @Override
    public CommFireHandle selectInfoByfireId(Long fireId) {
        return baseMapper.selectByfireId(fireId);
    }

    /**
     * 修改火情处理信息
     *
     * @param fireHandleInfo
     */
    @Override
    public void updateData(CommFireHandle fireHandleInfo) {
        commFireAlarmDao.updatePrimaryLevel(fireHandleInfo.getFireId(), fireHandleInfo.getFireJudgmentLevel());
        if (baseMapper.updateById(fireHandleInfo) <= 0) {
            throw new ForestBaseException("修改火情处理信息失败");
        }
    }

    /**
     * 根据ID删除火情处理信息
     *
     * @param id
     */
    @Override
    public void deleteById(String id) {
        if (baseMapper.deleteById(id) <= 0) {
            throw new ForestBaseException("删除火情处理信息失败");
        }
    }

    @Override
    public void deleteByFireId(Long fireId) {
        QueryWrapper<CommFireHandle> wrapper = new QueryWrapper<>();
        wrapper.eq("fire_id", fireId);
        baseMapper.delete(wrapper);
    }

}
